/*
 *      SRegistryObj.h
 *
 *      Copyright 2009 virpool <virpool@ukrpost.ua>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include "SRegistryObj.h"

SRegistryObj::~SRegistryObj()
{
	 registry.clear();
	 //cout << "Registry Object: deleted correctly!\n";
}

int SRegistryObj::Add(string name, SObject *obj)
{
	map<string, s_obj*>::iterator p = registry.find(name);
	if (p == registry.end())
	{
		s_obj *varbl = new s_obj();
		varbl->obj = obj;
		varbl->clinks = 1;
		registry.insert(pair<string, s_obj*>(name, varbl));
		return S_ADD_VARIABLE_OK;
	}
	else
	{
		return S_VARIABLE_DUBLICATE;
	}
}

SObject* SRegistryObj::Get(string name)
{
	map<string, s_obj*>::iterator p = registry.find(name);
	if (p == registry.end())
	{
		return NULL;
	}
	return p->second->obj;
}

int SRegistryObj::Remove(string name)
{
	map<string, s_obj*>::iterator p = registry.find(name);
	if (p == registry.end())
	{
		return S_NO;
	}
	registry.erase(p);
	return S_YES;
}

void SRegistryObj::TestZombie()
{
	map<string, s_obj*>::iterator p = registry.begin();
	while (p != registry.end())
	{
		if (p->second->clinks == 0)
		{
			registry.erase(p);
		}
		p++;
	}
}
